package com.yanqu.road.server.pb;

import com.yanqu.road.entity.enums.eDecorationType;
import com.yanqu.road.entity.enums.eIdentityType;
import com.yanqu.road.entity.enums.eMessageType;
import com.yanqu.road.entity.message.BanChatUserData;
import com.yanqu.road.entity.message.WorldMessage;
import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.cross.activity.CrossActivityProto;
import com.yanqu.road.pb.player.PlayerProto;
import com.yanqu.road.pb.worldmessage.WorldMessageProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.DecorationModule;
import com.yanqu.road.server.manger.activity.tongbing.TongBingMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.utils.string.StringUtils;

public class WorldMessagePb {

    public static CrossActivityProto.CrossActivityChatReqMsg.Builder getCrossActivityChatReqMsg(int activityId, int type, int contentType, String content, long nowTime) {
        CrossActivityProto.CrossActivityChatReqMsg.Builder reqMsg = CrossActivityProto.CrossActivityChatReqMsg.newBuilder();
        reqMsg.setActivityId(activityId);
        reqMsg.setMsg(parseWorldMessageTempMsg(activityId, type, contentType, content, nowTime));
        return reqMsg;
    }

    public static WorldMessageProto.WorldMessageTempMsg.Builder parseWorldMessageTempMsg(int activityId, int type, int contentType, String content, long nowTime) {
        WorldMessageProto.WorldMessageTempMsg.Builder msg = WorldMessageProto.WorldMessageTempMsg.newBuilder();
        msg.setActivityId(activityId);
        msg.setType(type);
        msg.setContentType(contentType);
        msg.setType(eMessageType.HuaShiGangSystemMsg.getValue());
        msg.setContent(content);
        msg.setSendTime(nowTime);
        return msg;
    }

    public static WorldMessageProto.WorldMessageTempMsg.Builder parseWorldMessageTempMsg(UserInfo userInfo, int type, int contentType, String content, long nowTime,
            int activityId, String banquetId) {
        WorldMessageProto.WorldMessageTempMsg.Builder msg = WorldMessageProto.WorldMessageTempMsg.newBuilder();
        msg.setType(type);
        msg.setContentType(contentType);
        if (content != null) {
            msg.setContent(content);
        }

        msg.setSendTime(nowTime / 1000);
        msg.setUserId(userInfo.getUserId());
        PlayerProto.PlayerBaseTempMsg.Builder baseMsg = PlayerBasePb.parsePlayerBaseTempMsg(UserMgr.parseUserBaseInfo(userInfo, GameServer.getInstance().getServerId()));
        msg.setPlayerBaseData(baseMsg);
        msg.setActivityId(activityId);
        if (banquetId != null) {
            msg.setBanquetId(banquetId);
        }
        // 处理身份标识
        handleIdentity(userInfo, msg);
        return msg;
    }

    public static WorldMessageProto.WorldMessageTempMsg.Builder parseWorldMessageTempMsg(WorldMessage worldMessage) {
        if(null == worldMessage){
            return null;
        }
        if(worldMessage.getSendTime() <= 0){
            return null;
        }
        WorldMessageProto.WorldMessageTempMsg.Builder msg = WorldMessageProto.WorldMessageTempMsg.newBuilder();
        msg.setType(worldMessage.getType());
        msg.setContentType(worldMessage.getContentType());
        msg.setContent(worldMessage.getContent());
        msg.setSendTime(worldMessage.getSendTime());
        msg.setUserId(worldMessage.getUserId());
        UserInfo userInfo = null;
        if(worldMessage.getUserId() > 0){
            userInfo = UserMgr.getUserInfo(worldMessage.getUserId());
        }
        if(null != userInfo) {
            PlayerProto.PlayerBaseTempMsg.Builder baseMsg = PlayerBasePb.parsePlayerBaseTempMsg(UserMgr.parseUserBaseInfo(userInfo, GameServer.getInstance().getServerId()));
            msg.setPlayerBaseData(baseMsg);
            // 处理身份标识
            handleIdentity(userInfo, msg);
        }
        return msg;
    }

    public static void handleIdentity(UserInfo userInfo, WorldMessageProto.WorldMessageTempMsg.Builder msg) {
        if (userInfo == null) {
            return;
        }
        // 判断是否指导员,百晓生走原逻辑
        if (userInfo.isInstructor()) {
            msg.addIdentityType(eIdentityType.INSTRUCTOR.getValue());
        }
        //聊天称号走装饰功能。这里不处理20230813 hyx
//        // 判断是否城主
//        if (TongBingMgr.isTongBingMaster(userInfo.getUserId())) {
//            msg.addIdentityType(eIdentityType.MASTER.getValue());
//        }
//        // 判断是否为九龙寨主
//        GamePlayer onlinePlayer = GamePlayerMgr.getOnlinePlayer(userInfo.getUserId());
//        if (onlinePlayer != null) {
//            boolean hasDecoration = onlinePlayer.getModule(DecorationModule.class).hasDecoration(eDecorationType.ChatTitle.getValue(), 106800);
//            if (hasDecoration) {
//                msg.addIdentityType(eIdentityType.KOWLOON.getValue());
//            }
//        } else {
//
//        }
    }

    public static WorldMessageProto.RedHotNotifyMsg.Builder parseRedHotNotifyMsg(int type, int count, String param){
        WorldMessageProto.RedHotNotifyMsg.Builder msg = WorldMessageProto.RedHotNotifyMsg.newBuilder();
        msg.setType(type);
        msg.setCount(count);
        if(!StringUtils.isNullOrEmpty(param)){
            msg.setParam(param);
        }
        return msg;
    }

    public static WorldMessageProto.ChatControlUserTemp.Builder parseChatControlUserMsg(BanChatUserData userData){
        WorldMessageProto.ChatControlUserTemp.Builder builder = WorldMessageProto.ChatControlUserTemp.newBuilder();
        builder.setUserId(userData.getUserId());
        builder.setMessageType(userData.getMessageType());
        builder.setHiddenTimeParam(userData.getHiddenTimeParam());
        builder.setSilenceEndTime(userData.getSilenceEndTime());

        return builder;
    }
}
